package com.mrzhou.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrzhou.common.enums.system.MenuType;
import com.mrzhou.core.tool.api.CloudUser;
import com.mrzhou.core.tool.constants.StringSymbol;
import com.mrzhou.core.tool.utils.StringUtil;
import com.mrzhou.mybatis.support.ParentTree;
import com.mrzhou.system.entity.RoleMenu;
import com.mrzhou.system.mapper.RoleMenuMapper;
import com.mrzhou.system.service.IRoleMenuService;
import com.mrzhou.system.vo.MenuVO;
import com.mrzhou.system.vo.MetaVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author qiang.zhou
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {

    public List<MenuVO> treeRoleMenu(CloudUser cloudUser) {
        List<MenuVO> list = this.baseMapper.findMenuByRoles(cloudUser.getRoleName());
        // 额外处理
        list.stream().forEach(var -> this.handlerMenuVO(var));
        return ParentTree.convertListToTree(list);
    }


    private void handlerMenuVO(MenuVO vo) {
        vo.setName(StringUtil.capitalize(vo.getPath()));
        vo.setMeta(new MetaVO(vo.getMenuName(), vo.getIcon()));
        if (vo.getParentId().equals(ParentTree.PARENT_ID_FLAG) && MenuType.M.equals(vo.getMenuType())) {
            vo.setPath(StringSymbol.BSLASH + vo.getPath());
        }
    }

}
